/*
 * Debug.h
 *
 *  Created on: Dec 12, 2011
 *      Author: kylintse
 */

#ifndef DEBUG_H_
#define DEBUG_H_

#include <QtCore>

#include "kthread.h"

class Debug;

class DebugThread : public kthread
{
public:
	DebugThread(Debug* debug,QObject* parent=0);
	~DebugThread();

protected:
	virtual void run();

private:
	Debug* m_debug;
};

enum dgtFuncCode
{
	dfcParmReadHardware=0x00,
	dfcParmReadDevice=0x01,
	dfcParmReadHy103=0x02,
	dfcParmReadDevProtocol=0x03,

	dfcParmWriteHardware=0x10,
	dfcParmWriteDevice=0x11,
	dfcParmWriteHy103=0x12,
	dfcParmWriteDevProtocol=0x13,

	dfcMonHy103Eth0=0x20,
	dfcMonHy103Eth1=0x21,

	dfcMonHy103BasicInfoEth0=0x22,
	dfcMonHy103UdpEth0=0x23,
	dfcMonHy103TcpSendEth0=0x24,
	dfcMonHy103TcpRecvEth0=0x25,

	dfcMonHy103BasicInfoEth1=0x28,
	dfcMonHy103UdpEth1=0x29,
	dfcMonHy103TcpSendEth1=0x2A,
	dfcMonHy103TcpRecvEth1=0x2B,

	dfcMonCan0=0x30,

	dfcMonCan0BasicInfo=0x31,
	dfcMonCan0Send=0x32,
	dfcMonCan0Recv=0x33,

	dfcMonCan1=0x40,

	dfcMonCan1BasicInfo=0x41,
	dfcMonCan1Send=0x42,
	dfcMonCan1Recv=0x43,

	dfcSectorList=0xF0,
	dfcChangeSector=0xF1,
	dfcBasicStatus=0xFE,
	dfcConfirm=0xFF
};

class Debug : public kthread
{
	Q_OBJECT
public:
	Debug();
	virtual ~Debug();

public:
	int processRecv();
	void closeClient();

public slots:
	void sendData(dgtFuncCode code,QByteArray data);

protected:
	virtual void run();

	void processData();

	void sendConfirm(qint8 confirm);

private:
	int m_listenSockFd;
	int m_clientSockFd;
	DebugThread* m_thread;
	QByteArray m_data;
	char m_recvBuf[2048];
	int m_curDataLen;
	int m_lenWant;
};

#endif /* DEBUG_H_ */
